<?php

namespace App\Http\Controllers\PersonInfoManagement;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Database\QueryException;
use App\Models\PersonInfoManagement\Temporary;
use App\Models\PersonInfoManagement\TemporaryFile;
use Illuminate\Support\Facades\DB;

/**
 * @group 人事管理->临时用工管理
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */

class TemporaryController extends Controller
{
    /**
     * 临时用工查看列表
     *
     * @queryParam name string required 员工姓名
     * @queryParam sex char required 性别 0男 1女
     * @queryParam id_no string required 证件号码
     * @queryParam id_start_time date required 证件生效日期
     * @queryParam id_end_time date required 证件失效日期
     * @queryParam phone string required 电话号码
     * @queryParam bank_name text required 开户行
     * @queryParam account string required 银行账号
     * @queryParam organization_id int required 所属组织 外键 组织表
     * @queryParam department_id int required 所属部门 外键 部门 表
     * @queryParam post_id int required 岗位 外键 岗位表
     * @queryParam skill_id int required 职级 外键 职级表
     * @queryParam payroll int required 工薪
     * @queryParam wageCategory int required 工薪类型 0时薪 1日薪
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $temporary = new Temporary();
        $data = $temporary->getList($request);
        return response()->json(encodeMsg($data,200));
    }

    /**
     * 新增临时用工
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     *
     * @bodyParam name string required 员工姓名
     * @bodyParam sex char required 性别 0男 1女
     * @bodyParam id_no string required 证件号码
     * @bodyParam id_start_time date required 证件生效日期
     * @bodyParam id_end_time date required 证件失效日期
     * @bodyParam phone string required 电话号码
     * @bodyParam bank_name text required 开户行
     * @bodyParam account string required 银行账号
     * @bodyParam organization_id int required 所属组织 外键 组织表
     * @bodyParam department_id int required 所属部门 外键 部门表
     * @bodyParam post_id int required 岗位 外键 岗位表
     * @bodyParam skill_id int required 职级 外键 职级表
     * @bodyParam payroll int required 工薪
     * @bodyParam wageCategory int required 工薪类型 0时薪 1日薪
     * @bodyParam temporary_ids string required 证件附件ids:1,2,3
     *
     * @response 201 {"code":201,"msg":"created","data":"新增成功"}
     * @response 400 {"code":400,"msg":"param error","data":"新增失败"}
     */
    public function store(Request $request)
    {
        DB::beginTransaction();
        try {
            $data = $request->all();
            $location = $request->input('location','');
            if(!empty($location)){
                $data['location'] = Point::fromString($location);
            }
            $data = Temporary::create($data);
            if($request->has('temporary_ids')){
                $file_data = [];
                $fileArr = explode(',',$request->get('temporary_ids'));
                foreach ($fileArr as $k=>$value){
                    $tmp = [];
                    $tmp['file_id'] = $value;
                    $tmp['temporary_id'] = $data->id;
                    array_push($file_data,$tmp);
                }
                try{
                    TemporaryFile::insert($file_data);
                }catch (\Exception $e){
                    DB::rollBack();
                    return response()->json(encodeMsg(["message" => "请先确认图片是否已经添加或者图片id是否重复".$e->getMessage()],404), 404);
                }
            }
            DB::commit();
            return response()->json(encodeMsg($data,201));
        } catch (QueryException $e) {
            DB::rollBack();
            // dd($e);
            return response()->json(encodeMsg(['message' => '临时用工信息添加失败!'],400));
        }
    }

    /**
     * 查看临时用工
     *
     * @bodyParam id int required 临时用工ID
     *
     * @queryParam name string required 员工姓名
     * @queryParam sex char required 性别 0男 1女
     * @queryParam id_no string required 证件号码
     * @queryParam id_start_time date required 证件生效日期
     * @queryParam id_end_time date required 证件失效日期
     * @queryParam phone string required 电话号码
     * @queryParam bank_name text required 开户行
     * @queryParam account string required 银行账号
     * @queryParam organization_id int required 所属组织 外键 组织表
     * @queryParam department_id int required 所属部门 外键 部门 表
     * @queryParam post_id int required 岗位 外键 岗位表
     * @queryParam skill_id int required 职级 外键 职级表
     * @queryParam payroll int required 工薪
     * @queryParam wageCategory int required 工薪类型 0时薪 1日薪
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = Temporary::with('files')->find($id);
        if($data == []) {
            return response()->json(encodeMsg(["message" => "没有该临时用工信息!"], 404));
        } else {
            return response()->json(encodeMsg($data,200));
        }
    }

    /**
     * 修改临时用工
     *
     * @bodyParam id int required 临时用工ID
     * @bodyParam name string required 员工姓名
     * @bodyParam sex char required 性别 0男 1女
     * @bodyParam id_no string required 证件号码
     * @bodyParam id_start_time date required 证件生效日期
     * @bodyParam id_end_time date required 证件失效日期
     * @bodyParam phone string required 电话号码
     * @bodyParam bank_name text required 开户行
     * @bodyParam account string required 银行账号
     * @bodyParam organization_id int required 所属组织 外键 组织表
     * @bodyParam department_id int required 所属部门 外键 部门表
     * @bodyParam post_id int required 岗位 外键 岗位表
     * @bodyParam skill_id int required 职级 外键 职级表
     * @bodyParam payroll int required 工薪
     * @bodyParam wageCategory int required 工薪类型 0时薪 1日薪
     * @bodyParam  temporary_ids   string required  证件附件ids:1,2,3
     *
     * @queryParam name string required 员工姓名
     * @queryParam sex char required 性别 0男 1女
     * @queryParam id_no string required 证件号码
     * @queryParam id_start_time date required 证件生效日期
     * @queryParam id_end_time date required 证件失效日期
     * @queryParam phone string required 电话号码
     * @queryParam bank_name text required 开户行
     * @queryParam account string required 银行账号
     * @queryParam organization_id int required 所属组织 外键 组织表
     * @queryParam department_id int required 所属部门 外键 部门 表
     * @queryParam post_id int required 岗位 外键 岗位表
     * @queryParam skill_id int required 职级 外键 职级表
     * @queryParam payroll int required 工薪
     * @queryParam wageCategory int required 工薪类型 0时薪 1日薪
     *
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $temporary = Temporary::find($id);
        if($temporary == []) {
            return response()->json(encodeMsg(["message" => "没有该临时用工信息!"], 404));
        }
        DB::beginTransaction();
        try {
            $data = $request->all();
            $location = $request->input('location','');
            if(!empty($location)){
                $data['location'] = Point::fromString($location);
            }
            if($request->has('temporary_ids')){
                $origin_files = TemporaryFile::where('temporary_id',$id)->get()->toArray();//图片原来的id号是好多 取出
                $origin_file_ids = array();//这个数组用于报错以前的所有file的id号
                if($origin_files != []){
                    foreach ($origin_files as $v){
                        array_push($origin_file_ids,$v['id']);
                    }
                }
                $file_data = [];
                $fileArr = explode(',',$request->get('temporary_ids'));
                foreach ($fileArr as $k=>$value){
                    $tmp = [];
                    $tmp['file_id'] = $value;
                    $tmp['temporary_id'] = $id;
                    array_push($file_data,$tmp);
                }
                try{
                    TemporaryFile::whereIn('id',$origin_file_ids)->delete();
                    TemporaryFile::insert($file_data);
                }catch (\Exception $e){
                    DB::rollBack();
                    return response()->json(encodeMsg(["message" => "请先确认图片是否已经添加或者图片id是否重复".$e->getMessage()],404), 404);
                }
            }
            $temporary->update($data);
            DB::commit();
            return response()->json(encodeMsg($temporary,200));
        } catch (QueryException $e) {
            DB::rollBack();
            // dd($e);
            return response()->json(encodeMsg(['message' => '临时用工信息修改失败!'],400));
        }
    }

    /**
     * 删除临时用工
     *
     *
     * @bodyParam id int required  临时用工ID
     * @response 204 {
     *   "message": "站点删除成功"
     * }
     *
     *
     * @response 404 {
     *  "message": "没有该站点信息"
     * }
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $temporary = Temporary::find($id);
        if($temporary == []) {
            return response()->json(encodeMsg(["message" => "没有该临时用工信息!"], 404));
        }
        if($temporary->delete()) {
            return response()->json(encodeMsg(["message" => "临时用工信息删除成功!"], 204));
        } else {
            return response()->json(encodeMsg(["message" => "临时用工信息删除失败"], 400));
        }
    }

    /**
     * 批量删除临时用工
     *
     * @bodyParam  ids int required  id集合字符串[格式：ids=1,2,3]
     * @response 204 {
     *   "message": "临时用工信息删除成功"
     * }
     *
     *
     */
    public function delete($ids)
    {
        $listId = explode(',', $ids);
        Temporary::whereIn('id',$listId)->delete();
        return response()->json(encodeMsg(["message" => "临时用工信息删除成功!"], 204));
    }
}
